<?php

/**
 * 分页
 *
 * @author itwhen
 * @param array  $params
 * @param object $smarty
 * @return string
 */
function smarty_function_pager ($params, &$smarty)
{
    /*
    @param  mixed   $num_data          - number of data
    @param  int     $num_limit 				- number of data each page
    @param  int     $num_link            - max. number of links to show on one page (default: 5)
    @param  int     $page_current            - current page number
    @param  string  url             - baseurl to which the pagenumber will appended
    @param  string  url_append          - text to append to url after pagenumber, e.g. "html" (default: "")
    @param  string  txt_first           - text for link to first page (default: "&&")
    @param  string  txt_prev            - text for link to previous page (default: "&")
    @param  string  separator           - text to print between page numbers (default: "&|&")
    @param  string  txt_next            - text for link to next page (default: "&")
    @param  string  txt_last            - text for link to last page (default: "&&")
    @param  string  txt_skip            - text shown when page s are skipped (not shown) (default: "&...&")
    @param  string  css_class           - css class for the pager (default: "")
    @param  boolean link_current        - whether to link the current page (default: false)
    */

    /* Define all vars with default value */
    $num_link = 10;
    $url_append= '';
    $txt_first = '|&lsaquo;';
    $txt_prev  = '&lsaquo;&lsaquo;';
    $txt_next  = '&rsaquo;&rsaquo;';
    $txt_last  = '&rsaquo;|';
    $link_current = false;

    extract($params);

    $target = isset($target) ? " target=\"$target\"" : '';

    /* Check parameters */
    if (empty($num_data)) {
        return;
    }
    if (empty($num_limit)) {
        $smarty->trigger_error("sliding_pager: missing 'num_limit' parameter");
        return;
    }
    if (empty($page_current)) {
        $smarty->trigger_error("sliding_pager: missing 'page_current' parameter");
        return;
    }
    $basicUrl = $url;
    if (empty($url)) {
        $url = '?p=';
    }else {
        if (strpos($url, 'p=')=== false) {
            if (strpos($url, '?')===false) {
                $url .= '?p=';
            } else {
                list($l, $r) = explode('?', $url);
                    $url .=  ($r ? '&p=' : 'p=');
            }
        }
    }

    $pagenum_data = ceil($num_data/$num_limit);

    if ($pagenum_data <= 1) {
        //return;
    }
    $page_current = (int)$page_current;
    if ($page_current < 1 || $page_current > $pagenum_data){
    	$page_current = 1;
    }

    if ($num_link % 2 == 0) {
        $dl = $num_link/2-1;
        $dr = $num_link/2;
    } else {
        $dl = $dr = ($num_link-1)/2;
    }

    $page_start = $page_current - $dl;
    $page_end = $page_current + $dr;

    for (;$page_start<1;$page_start++,$page_end++);
    for (;$page_end>$pagenum_data;$page_end--,$page_start--);
    $page_start<=0 && $page_start = 1;

    $links = range($page_start, $page_end);
    $retval = '<ul class="paginator"><li>';
    /* Build link
                    	<a href="#" title="<<"><<</a>
                        <a href="#" title="1" class="default">1</a>
                        <a href="#" title="2">2</a>
                        <a href="#" title="3">3</a>
                        <a href="#" title="4">4</a>
                        <a href="#" title="5">5</a>
                        <a href="#" title="6">6</a>
                        <a href="#" title="7">7</a>
                        <a href="#" title="8">8</a>
                        <span>...</span>
                        <a href="#">>></a>
                    </li>
                    <li>共100页</li>
                    <li>到第</li>
                    <li style="padding:0;"><input type="text" class="pageinput" /></li>
                    <li>页</li>
                    </ul>


    */
    if ($page_current-$dl>2){
        $retval .= '<a href="'.$url.'1'.$url_append.'" rel="nofollow">'.$txt_first.'</a>';
    }
    //if ($page_current > 1) {
        $retval .= '<a href="'.$basicUrl.'" rel="nofollow">'.$txt_prev.'</a>';
    //}
    $count = count($links);
    for($i = 0; $i < $count; $i++) {
        if ($links[$i] == 1) {
            $retval .= '<a href="'.$basicUrl.'" rel="nofollow">'.$links[$i].'</a>';
        }elseif (($links[$i] != $page_current) || $link_current) {
            $retval .= '<a href="'.$url.$links[$i].$url_append.'" rel="nofollow">'.$links[$i].'</a>';
        } else {
            $retval .= '<a style="font-size: 15px;background:#FA8072;" rel="nofollow">'. $links[$i]. '</a>';
        }
    }
    if ($page_current != $pagenum_data) {
        $retval .= '<a href="'.$url.($page_current + 1).$url_append.'" rel="nofollow">'.$txt_next.'</a>';
    }
    if ($page_current<=$pagenum_data-$dr-1){
        $retval .= '<a href="'.$url.$pagenum_data.$url_append.'" rel="nofollow">'.$txt_last.'</a>';
    }
    $retval.= '<li>共' . $num_data . '条</li><li> ' . $page_current .'/'. $pagenum_data .' 页</li><li style="padding:0;">';
    return $retval;
}

/* vim: set expandtab: */
/* vim: set ts=4: */

?>
